import { loginAPI,getBuildingListAPI,delCardAPI,getCardDetailAPI,delCardListAPI } from '@/api/user'
import Message from 'element-ui'
import router from '@/router'
export default {
  namespaced: true,
  state: () => {
    return {
      token: ''
    }
  },
  mutations: {
    getToken(state, token) {
      state.token = token
    },
    // 退出设置，清除用户信息
    clearUserInfo(state) {
      // 清除token
      state.token = ''
      // 清除localStorage
      localStorage.removeItem('token')
      // 清除sessionStorage
      sessionStorage.clear()
      // 重置所有用户相关状态
      state = Object.assign(state, {
        token: '',
        userInfo: null,
        permissions: []
      })
    }
  },
  actions: {
    // 登录功能函数
    async doLogin(content, { username, password }) {
      // 1. 调用登录接口,返回得到token
      const { data } = await loginAPI({ username, password })
      // 2. 保存token到state中
      console.log(data.token)
      content.commit('getToken', data.token)
    },
    // 获取月卡信息列表数据
    async getCardList(content, params) {
      const { data } = await getBuildingListAPI(params)
      return data
    },

    // 删除月卡
    async delCard(content, id) {
      const { data } = await delCardAPI(id)
      return data
    },

    // 获取编辑月卡详情后，用于之后回显到组件中
    async getCardDetail(content, id) {
      const { data } = await getCardDetailAPI(id)
      return data
    },

// 批量删除月卡
    async delCardList(content, idList) {
      try {
        const { data } = await delCardListAPI(idList)
        return data
      } catch (error) {
        return Promise.reject(error)
      }
    },
   
  }
}
